package com.fushenlan.epidemicControl.service.Impl;

import com.fushenlan.Vo.NeighborhoodIndoVo;
import com.fushenlan.epidemicControl.dto.NeighborhoodInfoDto;
import com.fushenlan.epidemicControl.mapper.NeighborhoodInfoMapper;
import com.fushenlan.epidemicControl.service.NeighborhoodInfoService;
import com.fushenlan.helper.ListPageHelper;
import com.fushenlan.po.NeighborhoodInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author XUJIN
 * @DATE 2022/4/21
 * @PACKAGE_NAME com.fushenlan.epidemicControl.service.Impl
 * @VERSION 1.0
 */
@Service
public class NeighborhoodInfoServiceImpl implements NeighborhoodInfoService {

    @Autowired
    private NeighborhoodInfoMapper neighborhoodInfoMapper;


    /**
     * 查询居委信息列表，可根据传入的参数条件 动态查询
     * @return
     */
    @Override
    public List<NeighborhoodIndoVo> getNeighborList(NeighborhoodInfoDto neighborhoodInfoDto) {

        NeighborhoodInfo neighborhoodInfo = new NeighborhoodInfo();
        //将前端传入的数据映射实体，便于查询
        BeanUtils.copyProperties(neighborhoodInfoDto,neighborhoodInfo);
        List<NeighborhoodInfo> neighborList = neighborhoodInfoMapper.findNeighborList(neighborhoodInfo);
        List<NeighborhoodIndoVo> list = new ArrayList<>();
        if (!neighborList.isEmpty()){
            for (NeighborhoodInfo tmp : neighborList){
                NeighborhoodIndoVo neighborhoodIndoVo = new NeighborhoodIndoVo();
                BeanUtils.copyProperties(tmp,neighborhoodIndoVo);
                list.add(neighborhoodIndoVo);
            }
        }
        return list;
    }

    /**
     * 添加居委会
     * @param neighborhoodInfoDto
     * @return 受影响行数
     */
    @Override
    @Transactional
    public int addNeighbor(NeighborhoodInfoDto neighborhoodInfoDto) {

        NeighborhoodInfo neighborhoodInfo = new NeighborhoodInfo();
        BeanUtils.copyProperties(neighborhoodInfoDto,neighborhoodInfo);
        if (neighborhoodInfo.getCreateDate() == null){
            neighborhoodInfo.setCreateDate(new Date());
        }
        if (neighborhoodInfo.getUpdateDate() == null){
            neighborhoodInfo.setUpdateDate(new Date());
        }
        return neighborhoodInfoMapper.insert(neighborhoodInfo);
    }

    @Override
    @Transactional
    public int deleteNeighbor(Integer id) {
        return  neighborhoodInfoMapper.deleteById(id);
    }

    @Override
    public List<NeighborhoodIndoVo> getNeighborListByPage(int pageNo, int pageSize, List<NeighborhoodInfo> neighborhoodInfos) {
        List<?> listPage = ListPageHelper.getListPage(pageNo, pageSize, neighborhoodInfos);
        return (List<NeighborhoodIndoVo>) listPage;
    }

    @Override
    @Transactional
    public int updateNeighbor(NeighborhoodInfoDto neighborhoodInfoDto) {
        NeighborhoodInfo neighborhoodInfo = new NeighborhoodInfo();
        BeanUtils.copyProperties(neighborhoodInfoDto,neighborhoodInfo);
        return neighborhoodInfoMapper.updateById(neighborhoodInfo);
    }

    @Override
    public List<NeighborhoodInfo> findNeighborList(NeighborhoodInfo neighborhoodInfo) {
        return neighborhoodInfoMapper.findNeighborList(neighborhoodInfo);
    }
}
